package test.serverframe.armc.server.manager.dao.mapper;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SQL;
import test.serverframe.armc.server.manager.domain.Task;

import java.util.Map;

public class TaskSqlProvider {

    public String insertSelective(Task record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("tb_ssgp_task");

        if (record.getTaskId() != null) {
            sql.VALUES("task_id", "#{taskId,jdbcType=VARCHAR}");
        }

        if (record.getTaskName() != null) {
            sql.VALUES("task_name", "#{taskName,jdbcType=VARCHAR}");
        }

        if (record.getRemark() != null) {
            sql.VALUES("remark", "#{remark,jdbcType=VARCHAR}");
        }

        if (record.getCreateTime() != null) {
            sql.VALUES("create_time", "#{createTime,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateTime() != null) {
            sql.VALUES("update_time", "#{updateTime,jdbcType=TIMESTAMP}");
        }

        if (record.getTaskGroupId() != null) {
            sql.VALUES("task_group_id", "#{taskGroupId,jdbcType=VARCHAR}");
        }

        if (record.getVersion() != null) {
            sql.VALUES("version", "#{version,jdbcType=BIGINT}");
        }

        if (record.getIsUsed() != null) {
            sql.VALUES("is_used", "#{isUsed,jdbcType=BIT}");
        }
        
        if (record.getNodeArr() != null) {
            sql.VALUES("node_arr", "#{nodeArr,jdbcType=VARCHAR}");
        }

        return sql.toString();
    }

    public String updateByPrimaryKeySelective(Task record) {
        SQL sql = new SQL();
        sql.UPDATE("tb_ssgp_task");

        if (record.getTaskName() != null) {
            sql.SET("task_name = #{taskName,jdbcType=VARCHAR}");
        }

        if (record.getRemark() != null) {
            sql.SET("remark = #{remark,jdbcType=VARCHAR}");
        }

        if (record.getCreateTime() != null) {
            sql.SET("create_time = #{createTime,jdbcType=TIMESTAMP}");
        }

        if (record.getUpdateTime() != null) {
            sql.SET("update_time = #{updateTime,jdbcType=TIMESTAMP}");
        }

        if (record.getTaskGroupId() != null) {
            sql.SET("task_group_id = #{taskGroupId,jdbcType=VARCHAR}");
        }

        if (record.getVersion() != null) {
            sql.SET("version = #{version,jdbcType=BIGINT}");
        }

        if (record.getIsUsed() != null) {
            sql.SET("is_used = #{isUsed,jdbcType=BIT}");
        }
        
        if (record.getNodeArr() != null) {
            sql.SET("node_arr = #{nodeArr,jdbcType=VARCHAR}");
        }

        sql.WHERE("task_id = #{taskId,jdbcType=VARCHAR}");

        return sql.toString();
    }

    public String selectByPage(Map<String, Object> params) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT " +
                " task.*  " +
                "FROM " +
                " tb_ssgp_task task " +
                " INNER JOIN ( " +
                "SELECT " +
                " t.task_id AS task_id  " +
                "FROM " +
                " tb_ssgp_task t " +
                " INNER JOIN ( SELECT task_group_id, max( version ) AS version FROM tb_ssgp_task GROUP BY task_group_id ) v ON v.version = t.version  " +
                " ) new_version ON task.task_id = new_version.task_id  " +
                "WHERE 1 = 1 " +
                " and task.is_used = 1 ");
        Object name = params.get("taskName");
        if (name != null && StringUtils.isNotBlank(name.toString())) {
            sql.append(" and task.task_name like '%" + name.toString() + "%'");
        }
        sql.append(" ORDER BY ");
        sql.append(" task.update_time DESC");
        return sql.toString();
    }
}
